package com.uzai.console.service.wechatprivate.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.wechatprivate.wxq.wxgroupblacklist.baseconfig.WxqBlacklistBaseConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.redis.WxqBlacklistBaseConfigRedis;
import com.uzai.console.mapper.CikuKeywordsMapper;
import com.uzai.console.mapper.CikuKeywordsReplyMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.wechatprivate.WxqBlacklistBaseService;
import com.uzai.console.vo.wechatprivate.wxq.wxgroupblacklist.baseconfig.WxqBlacklistBaseConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class WxqBlacklistBaseServiceImpl implements WxqBlacklistBaseService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private CikuKeywordsReplyMapper cikuKeywordsReplyMapper;
    @Autowired
    private CikuKeywordsMapper cikuKeywordsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询社群黑名单基础设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public WxqBlacklistBaseConfigVo findWxqBlacklistConfig(Long merId){
        WxqBlacklistBaseConfigVo wxqBlacklistBaseConfigVo = new WxqBlacklistBaseConfigVo();
        //修改微信群黑名单配置
        MerBaseConfig wxq_blacklist_config = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.WXQ_BLACKLIST_CONFIG.getValue());
        if(wxq_blacklist_config != null && StringUtils.isNotBlank(wxq_blacklist_config.getVal())){
            wxqBlacklistBaseConfigVo =  JSONObject.parseObject(wxq_blacklist_config.getVal(), WxqBlacklistBaseConfigVo.class);
        }
        return wxqBlacklistBaseConfigVo;
    }

    /**
     * 修改社群黑名单基础设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wechatBlacklistBaseConfigDto
     * @return null
     */
    @Transactional
    public void updateWxqBlacklistConfig(WxqBlacklistBaseConfigDto wechatBlacklistBaseConfigDto){

        Integer now = DateUtil.getNowTime();

        //保存到Redis当中
        WxqBlacklistBaseConfigRedis wxqBlacklistBaseConfigRedis = new WxqBlacklistBaseConfigRedis();
        BeanUtils.copyProperties(wechatBlacklistBaseConfigDto, wxqBlacklistBaseConfigRedis);

        //修改会员黑名单配置
        MerBaseConfig wxq_blacklist_config = merBaseConfigMapper.selectByMerId(wechatBlacklistBaseConfigDto.getMerId(), MerConfigKeyEnum.WXQ_BLACKLIST_CONFIG.getValue());
        if (wxq_blacklist_config != null){
            wxq_blacklist_config.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wxqBlacklistBaseConfigRedis)));
            wxq_blacklist_config.setUpdateTime(now);
            merBaseConfigMapper.update(wxq_blacklist_config);
        } else { //增加会员黑名单配置
            wxq_blacklist_config = new MerBaseConfig();
            wxq_blacklist_config.setId(IdWorker.getId());
            wxq_blacklist_config.setMerId(wechatBlacklistBaseConfigDto.getMerId());
            wxq_blacklist_config.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            wxq_blacklist_config.setKey(MerConfigKeyEnum.WXQ_BLACKLIST_CONFIG.getValue());
            wxq_blacklist_config.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wxqBlacklistBaseConfigRedis)));
            wxq_blacklist_config.setCreateTime(now);
            wxq_blacklist_config.setUpdateTime(now);
            merBaseConfigMapper.insert(wxq_blacklist_config);
        }

        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.WXQ_BLACKLIST_CONFIG.getKey(), wechatBlacklistBaseConfigDto.getMerId()), Tools.getStrEmpty(JSONObject.toJSONString(wxqBlacklistBaseConfigRedis)));

    }

}
